const { findSourceMap } = require("module");

cc.Class({
    extends: cc.BaseClass,
    properties: {
        gameScrollView:     cc.ScrollView,
        atlas: cc.SpriteAtlas,
        prefabs:[cc.Prefab], //[创建房间] 
    },

    start () {
        this.registerEvent();
        this.initData();
    },

    onLoad:function(){
        HallEvent.getGameLiset();
    },

    registerEvent:function(){
        ClientNotify.register(CLIENT_NOTIFY_TYPE.SUB_GP_GET_LIST_REQ,this.onGetGameList,this);    
    },

    initData:function(){
        this.item = this.gameScrollView.content.children[0];
    },

    setGameList:function(data){
        if(!data) return;
        var content =  this.gameScrollView.content;
        var item = this.item;

      
        for(let i = 0; i <  data.Arr.length;i++){
            var node = cc.instantiate(item);
            content.addChild(node);
        }

        var length = data.Arr.length + 1;
        for(var i =0; i < length;i++){
            var item = content.children[i];
            var temp = data.Arr[i];
            var _name = "item";
            if(i == length - 1){
                var frame = this.atlas.getSpriteFrame("hall_gameMore");
            }else{
                var frameName = "hall_game"+temp.wKindID;
                var frame = this.atlas.getSpriteFrame(frameName);
                var _name = data.Arr[i].wKindID.toString();
            }
            if(frame){
                item.getComponent(cc.Sprite).spriteFrame = frame;
                item.setName(_name);
            }
        }
    },

    onBtnClickCallBack:function(event,customData){
        var name = event.target.name;
        console.log("========onBtnClickCallBack=================" +name);
        var parent = cc.director.getScene();
        var node = cc.instantiate(this.prefabs[0]);
        parent.addChild(node);
        node.getComponent("createRoom").initView(name);
    },

    onDestroy:function(){
        ClientNotify.removeByType(CLIENT_NOTIFY_TYPE.SUB_GP_GET_LIST_REQ);
    },
    //===============================获取服务器消息======================================
    onGetGameList:function(data,target){
        console.log("=======onGetGameList========");
        var self = target;
        self.setGameList(data);
    }
});
